package org.istt.web;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.istt.domain.Exchange;
import org.istt.domain.File;
import org.istt.domain.User;
import org.istt.service.ExchangeManager;
import org.istt.service.FileManager;
import org.istt.service.FileMgrManager;
import org.istt.web.helper.ExtendedPaginatedList;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

public class TaskAction extends BaseAction<FileManager, File, String>{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1444072119092590099L;
    
    private String commentId;
    
    private ExtendedPaginatedList exchangeMsgList;
    @Autowired
    private ExchangeManager exchangeMsgManager;
    
	private ExtendedPaginatedList fileList;
	@Autowired
    private FileManager fileManager;
	
	@Autowired
	private FileMgrManager fileMgrManager;
	
	private String filterType;
	
	private final Log log = LogFactory.getLog(TaskAction.class);
	private String replyMessage;
	private String totalFiles;
	private String totalFilesDeadline;
	private String totalFilesOvertime;
	private String totalRejectedFile;
	
	private User user;
	
	
	public String filter(){
		log.debug("enter method filter... ");
		//TODO: get filter type
		if(filterType!=null){
			if(WebConstants.TASKLIST_FILTER_TYPE_ALL.equals(this.filterType)){
				getTask(WebConstants.TASKLIST_FILTER_TYPE_ALL);
			}else if(WebConstants.TASKLIST_FILTER_TYPE_NEARLY_DEADLINE.equals(this.filterType)){
				getTask(WebConstants.TASKLIST_FILTER_TYPE_NEARLY_DEADLINE);
			}else if(WebConstants.TASKLIST_FILTER_TYPE_OVERDEADLINE.equals(this.filterType)){
				getTask(WebConstants.TASKLIST_FILTER_TYPE_OVERDEADLINE);
			}
		}
		return SUCCESS;
	}
	
	
	public String getCommentId() {
		return commentId;
	}
	
	/*
	 * Get list exchange message
	 */
	public void getExchangeMessage(Long userId){
		if(userId!=null){
			ExtendedPaginatedList paginatedList = paginateListFactory.getPaginatedListFromRequest(ServletActionContext.getRequest());
			this.exchangeMsgList = this.exchangeMsgManager.getLstExchangeMessageByReceiver(userId, paginatedList);
		}
	}

	public ExtendedPaginatedList getExchangeMsgList() {
		return exchangeMsgList;
	}
	
	public ExchangeManager getExchangeMsgManager() {
		return exchangeMsgManager;
	}

	public ExtendedPaginatedList getFileList() {
		return fileList;
	}
	public String getFilterType() {
		return filterType;
	}
	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getMessageList(){
		
		log.debug("enter method getTask... ");
		
		user = (User) ActionContext.getContext().getSession().get(WebConstants.USER_INFO);
		if(user!=null){
				//TODO: Load exchange messages
			getExchangeMessage(user.getId());
		}
		
		return SUCCESS;
	}
	public String getReplyMessage() {
		return replyMessage;
	}
	/***
	 * Get list files by filter condition
	 * @param filterCondition
	 */
	private void getTask(String filterCondition){
		log.debug("enter method getTask... ");
		
		user = (User) ActionContext.getContext().getSession().get(WebConstants.USER_INFO);
		if(user!=null){
			ExtendedPaginatedList paginatedList = paginateListFactory.getPaginatedListFromRequest(ServletActionContext.getRequest());
			try {
				fileList = fileManager.findFileByApprover(user.getId(), null, null, "false",filterCondition, paginatedList);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}		
				//Update statistic
				setTotalFilesDeadline(fileManager.getTotalRecordByFilterCondition(user.getId(), null, null, "false",WebConstants.TASKLIST_FILTER_TYPE_NEARLY_DEADLINE)+"");
				setTotalFiles(fileManager.getTotalRecordByFilterCondition(user.getId(), null, null, "false",WebConstants.TASKLIST_FILTER_TYPE_ALL)+"");
				setTotalFilesOvertime(fileManager.getTotalRecordByFilterCondition(user.getId(), null, null, "false",WebConstants.TASKLIST_FILTER_TYPE_OVERDEADLINE)+"");
		}
	}

	/***
	 * Get list file that current user have to process
	 */
	public String getTaskList() throws Exception{
		
		getTask(WebConstants.TASKLIST_FILTER_TYPE_ALL);
		
		return SUCCESS;
	}

	public String getTotalFiles() {
		return totalFiles;
	}

	public String getTotalFilesDeadline() {
		return totalFilesDeadline;
	}

	public String getTotalFilesOvertime() {
		return totalFilesOvertime;
	}

	public String sendReplyMsg(){
		if(!StringUtils.isEmpty(this.commentId) && !StringUtils.isEmpty(this.replyMessage)){
			if (log.isDebugEnabled()) {
				log.debug("enter method sendMessage || commentId is equal to " + commentId + " || message: "+replyMessage);
			}
			Exchange message = this.exchangeMsgManager.get(Long.parseLong(this.commentId));
			if(message!=null){
				
				try {
					message.setStatus(true);
					this.exchangeMsgManager.save(message);
					
					Exchange reply = new Exchange();
					reply.setComment(this.replyMessage);
					reply.setDate(new DateTime());
					reply.setFile(message.getFile());
					User user = (User) ActionContext.getContext().getSession().get(WebConstants.USER_INFO);
					reply.setSender(user);
					reply.setUser(message.getSender());
					reply.setStatus(false);
					this.exchangeMsgManager.save(reply);
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		
		return SUCCESS;
	}

	public void setCommentId(String commentId) {
		this.commentId = commentId;
	}

	public void setExchangeMsgList(ExtendedPaginatedList exchangeMsgList) {
		this.exchangeMsgList = exchangeMsgList;
	}

	public void setExchangeMsgManager(ExchangeManager exchangeMsgManager) {
		this.exchangeMsgManager = exchangeMsgManager;
	}

	public void setFileList(ExtendedPaginatedList fileList) {
		this.fileList = fileList;
	}

	public void setFilterType(String filterType) {
		this.filterType = filterType;
	}

	public void setReplyMessage(String replyMessage) {
		this.replyMessage = replyMessage;
	}

	public void setTotalFiles(String totalFiles) {
		this.totalFiles = totalFiles;
	}

	public void setTotalFilesDeadline(String totalFilesDeadline) {
		this.totalFilesDeadline = totalFilesDeadline;
	}

	public void setTotalFilesOvertime(String totalFilesOvertime) {
		this.totalFilesOvertime = totalFilesOvertime;
	}


	public void setTotalRejectedFile(String totalRejectedFile) {
		this.totalRejectedFile = totalRejectedFile;
	}

	public String getTotalRejectedFile() {
		return this.fileMgrManager.getAmendmentFileRecordCount(loggedUser.getId()) + "";
	}



}